/**
 *====================================================
 * 文件名称: EnterpriseGradeServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年10月24日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.member.service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.DateUtils;
import com.yuexunit.finance.member.entity.BankcodeEntity;
import com.yuexunit.finance.member.entity.EnterpriseGradeEntity;
import com.yuexunit.finance.member.entity.EnterpriseModifyEntity;
import com.yuexunit.finance.zhanghuguangli.entity.EnterpriseAccountEntity;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;

/**
 * @ClassName: EnterpriseGradeServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年10月24日 19:46:13
 */
@Implements(EnterpriseGradeService.class)
public class EnterpriseGradeServiceImpl implements EnterpriseGradeService {

	@Override
	@Transactional
	public void createEnterpriseGrade(EnterpriseGradeEntity entity) {
		PersistUtil.insert(entity);
	}

	@Override
	@Transactional
	public void persistEnterpriseGrade(EnterpriseGradeEntity entity) {
		PersistUtil.update(entity);
	}

	@Override
	@Transactional
	public void deleteEnterpriseGrade(Long enterpriseId) {
		EnterpriseGradeEntity persist = inquireEnterpriseGrade(enterpriseId);
		if (persist == null) {
			return;
		}
		persist.setCreditLine(BigDecimal.ZERO);
		persist.setGradeName("");
		persist.setInterestRate(BigDecimal.ZERO);
		persist.setPenalInterest(BigDecimal.ZERO);
		PersistUtil.update(persist);
	}

	@Override
	public EnterpriseGradeEntity inquireEnterpriseGrade(Long enterpriseId) {
		return PersistUtil.get(EnterpriseGradeEntity.class, enterpriseId);
	}
	
	@Override
	public EnterpriseModifyEntity queryEnterpriseGrade(Long authenticationId) {
		return PersistUtil.get(EnterpriseModifyEntity.class, authenticationId);
	}
	
	@Override
	public EnterpriseAccountEntity queryAccountGrade(Long accountId) {
		return PersistUtil.get(EnterpriseAccountEntity.class, accountId);
	}
	
	@Override
	public BankcodeEntity queryBankcodeGrade(Long lineNumberId) {
		return PersistUtil.get(BankcodeEntity.class, lineNumberId);
	}

	@Override
	public List<Long> countEnterpriseByLeftTime(BigDecimal alreadyCreditStart, BigDecimal alreadyCreditEnd, Integer leftTime) {
		DetachedCriteria criteria = DetachedCriteria.forClass(EnterpriseGradeEntity.class);
		if (leftTime != null) {
			criteria.add(Restrictions.le("effectiveEndDate", DateUtils.parseDateInt(DateUtils.addDateField(DateUtils.now(), Calendar.DATE, leftTime))));
		}
		if (alreadyCreditEnd != null) {
			criteria.add(Restrictions.le("creditAlready", alreadyCreditEnd.multiply(new BigDecimal(10000))));
		}
		if (alreadyCreditStart != null) {
			criteria.add(Restrictions.ge("creditAlready", alreadyCreditStart.multiply(new BigDecimal(10000))));
		}
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("enterpriseId")));
		return PersistUtil.get(criteria);
	}
}
